package com.hospital.demo.service.impl.jiashuai;

import com.hospital.demo.enums.ExceptionCodeEnums;
import com.hospital.demo.enums.UserType;
import com.hospital.demo.exception.HospitalException;
import com.hospital.demo.mapper.dingyuxin.PeopleMapper;
import com.hospital.demo.mapper.jiashuai.ITWorkerDao;
import com.hospital.demo.mapper.jiashuai.IWorkerDao;
import com.hospital.demo.mapper.jiashuai.PasswordMapper;
import com.hospital.demo.model.dingyuxin.People;
import com.hospital.demo.model.jiashuai.Password;
import com.hospital.demo.model.jiashuai.TWorker;
import com.hospital.demo.model.jiashuai.Worker;
import com.hospital.demo.service.jiashuai.LoginSer;
import com.hospital.demo.util.KeyUtils;
import com.hospital.demo.util.RedisUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginSerImpl implements LoginSer {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private PasswordMapper passwordMapper;

    @Autowired
    private PeopleMapper peopleMapper;

    @Autowired
    private ITWorkerDao itWorkerDao;

    @Autowired
    private IWorkerDao iWorkerDao;

    @Override
    public String login(int account, String password, int type) {
        String k = passwordMapper.selectByTypeAndNum(type, account).getSalt();
        String md = DigestUtils.md5Hex(password + k);
        Password p = passwordMapper.selectByNumAndPasswordAndType(account, md, type);
        if (p == null) {
            throw new HospitalException(ExceptionCodeEnums.DATA_NOT_FOUND);
        }
        String token = KeyUtils.getRandomString(10);
        redisUtil.set(token, type + "" + account, 3600);
        return token;
    }

    @Override
    public int resign(int account, String password, int type) {
        //检查是否存在相同账户
        Password p = passwordMapper.selectByTypeAndNum(type, account);
        if (p != null) {
            throw new HospitalException(ExceptionCodeEnums.DATA_NOT_FOUND);
        }
        //根据用户类型向对应的表添加人员
        if (type == UserType.INSURANCE_PEOPLE.getCode()) {
            People s = new People();
            s.setPeopleaccount(account);
            peopleMapper.insertSelective(s);
        } else if (type == UserType.WORKER.getCode()) {
            Worker w = new Worker();
            w.setDid(account);
            iWorkerDao.insertworker(w);
        } else if (type == UserType.TEMPORARY_WORKER.getCode()){
            TWorker t = new TWorker();
            t.setDeid(account);
            itWorkerDao.insert(t);
        }else if (type == UserType.ADMINISTRATORS.getCode()){

        }else {
            throw new HospitalException(ExceptionCodeEnums.DATA_NOT_FOUND);
        }

        //生成随机字符串 加密
        String k = KeyUtils.getRandomString(10);
        p = new Password();
        p.setAccount(account);
        p.setType(type);
        p.setSalt(k);
        p.setPassword(DigestUtils.md5Hex(password + k));
        passwordMapper.insertSelective(p);
        return 0;
    }

    @Override
    public String check(String token, int type) {
        //验证token
        String v = (String) redisUtil.getTrim(token);
        if (token == null || v == null || "".equals(v)){
            throw new HospitalException(ExceptionCodeEnums.DATA_NOT_FOUND);
        }
        System.out.println();
        if (type == UserType.INSURANCE_PEOPLE.getCode() &&
                String.valueOf(v.charAt(0)).equals(String.valueOf(UserType.INSURANCE_PEOPLE.getCode()))){
            redisUtil.set(token,v,3600);
            return v.substring(1);
        }

        if (type == UserType.WORKER.getCode() &&
                String.valueOf(v.charAt(0)).equals(String.valueOf(UserType.WORKER.getCode()))){
            redisUtil.set(token,v,3600);
            return v.substring(1);
        }
        if (type == UserType.TEMPORARY_WORKER.getCode() &&
                String.valueOf(v.charAt(0)).equals(String.valueOf(UserType.TEMPORARY_WORKER.getCode()))){
            redisUtil.set(token,v,3600);
            return v.substring(1);
        }

        if (type == UserType.ADMINISTRATORS.getCode() &&
                String.valueOf(v.charAt(0)).equals(String.valueOf(UserType.ADMINISTRATORS.getCode()))){
            redisUtil.set(token,v,3600);
            return v.substring(1);
        }
        throw new HospitalException(ExceptionCodeEnums.DATA_NOT_FOUND);
    }
}
